package net.dengzixu.java.homework.period38;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 1.定义一个User类，属性有姓名，年龄，性别（性别用字符类型表示，m表示男，f表示女），
 * 电话，住址，定义初始化属性的构造方法，定义打印输出所有用户信息的show方法
 * 2.定义一个管理User的类UserManager类，使用ArrayList对象管理所有的用户，
 * 完成用户的添加，删除，以及查找，同时统计用户的平均年龄，统计注册用户男性和女性的数量。
 * 3.定义测试类，测试类中各功能
 *
 * @author Deng Zixu <admin@dengzixu.com>
 */
public class TestUserManager {
    public static void main(String[] args) {
        UserManager userManager = new UserManager();

        User u1 = new User("用户1", 18, 'f', "18000000001", "地址1");
        User u2 = new User("用户2", 20, 'f', "18000000002", "地址2");
        User u3 = new User("用户3", 22, 'f', "18000000003", "地址3");
        User u4 = new User("用户4", 24, 'm', "18000000004", "地址4");
        User u5 = new User("用户5", 26, 'm', "18000000005", "地址5");
        User u6 = new User("用户6", 28, 'm', "18000000006", "地址6");

        userManager.add(u1, u2, u3, u4, u5, u6);

        System.out.println("用户平均年龄为: " + userManager.getAverageAge());

        System.out.println("用户2 的索引为:" + userManager.find(u3));

        userManager.remove(u3);

        userManager.totalGender();
    }

}

class UserManager {
    List<User> userList = new ArrayList<>();

    public void add(User user) {
        userList.add(user);
    }

    public void add(User... user) {
        userList.addAll(Arrays.asList(user));
    }

    public void remove(User user) {
        userList.remove(user);
    }

    public void remove(int index) {
        userList.remove(index);
    }

    public int find(User user) {
        if (userList.contains(user)) {
            return userList.indexOf(user);
        }
        return -1;
    }

    public int getAverageAge() {
        int totalAge = 0;

        for (User u : userList) {
            totalAge += u.age;
        }

        return totalAge / userList.size();
    }

    public void totalGender() {
        int maleCount = 0, femaleCount = 0;

        Iterator<User> userIterator = userList.iterator();
        while (userIterator.hasNext()) {
            if (userIterator.next().gender == 'm') {
                maleCount++;
            } else {
                femaleCount++;
            }
        }

        System.out.printf("男士数量: %d, 女士数量: %d", maleCount, femaleCount);
    }


}

class User {
    String name;
    int age;
    char gender;
    String tel;
    String address;

    public User(String name, int age, char gender, String tel, String address) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.tel = tel;
        this.address = address;
    }

    public void show() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                ", tel='" + tel + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
